
package com.saturday.common.util;

import cn.hutool.core.bean.BeanUtil;

import java.util.ArrayList;
import java.util.List;

public class BeanUtils extends BeanUtil {

    /**
     * <T>目标对象须含有无参构造器
     *
     * @param source 源对象
     * @param c      目标对象class
     * @param <T>    目标对象类型
     * @return
     * @throws IllegalAccessException
     * @throws InstantiationException
     */
    public static <T> T copyProperties(Object source, Class<T> c) {
        T t = null;
        try {
            t = c.newInstance();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        BeanUtil.copyProperties(source, t);
        return t;
    }

    /**
     * 列表数据拷贝
     *
     * @param list 源对象列表
     * @param c    目标对象class
     * @param <T>  目标对象类型
     * @return
     */
    public static <T> List<T> copyProperties(List<?> list, Class<T> c) {
        List<T> resultList = new ArrayList<>();
        for (Object obj : list) {
            T t = copyProperties(obj, c);
            resultList.add(t);
        }
        return resultList;
    }
}
